package org.example;

import java.io.*;

public class 第十八章_Java流Stream_文件File和IO {
    public static void main(String[] args) throws IOException {
//        p1();
//        p2();
        p3();
    }

    public static void p1() throws IOException {
        //从控制台读取
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("输入字符, 按下 'q' 键退出。");
        char c;
        // 读取字符
        do {
            c = (char) br.read();
            System.out.println(c);
        } while (c != 'q');
    }

    //读取和写入
    public static void p2() throws IOException {
        File file = new File("D:\\example-a\\foundation\\src\\main\\resources\\test.txt");
        InputStream inputStream = new FileInputStream(file);

        int size = inputStream.available();

        for (int i = 0; i < size; i++) {
            System.out.print((char) inputStream.read() + "  ");
        }

        //写入
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file));

        writer.write("1111111111111111");

        writer.flush();

        writer.close();
    }

    //文件与IO
    public static void p3(){
        File d = new File("D:\\tmp");
        //创建目录
        d.mkdir();
        //读取目录
        if (d.isDirectory()) {
            String s[] = d.list();
            for (int i = 0; i < s.length; i++) {
                File f = new File("D:\\tmp" + "\\" + s[i]);
                if (f.isDirectory()) {
                    System.out.println(s[i] + " 是一个目录");
                } else {
                    System.out.println(s[i] + " 是一个文件");
                }
            }
        } else {
            System.out.println("不是一个目录");
        }
    }
}
